﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

public partial class Manager_BaoMing : System.Web.UI.PageEx
{
    private string strPageCode = "B00001";
    private string strLoginResult = string.Empty;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            DataBinds();
            //绑定户籍类型
            ddlCensusClass.Items.Clear();
            //ddlCensusClass.Items.Add(new FineUI.ListItem("龙岗", "0"));
            ddlCensusClass.Items.Add(new FineUI.ListItem("深户", "1"));
            ddlCensusClass.Items.Add(new FineUI.ListItem("非深户", "2"));
            ddlCensusClass.Items.Add(new FineUI.ListItem("港籍", "3"));
            ddlCensusClass.Items.Add(new FineUI.ListItem("台籍", "4"));
            ddlCensusClass.Items.Add(new FineUI.ListItem("澳籍", "5"));
            ddlCensusClass.Items.Add(new FineUI.ListItem("外籍", "6"));
            ddlCensusClass.Items.Insert(0, new FineUI.ListItem("不限户籍", "-1"));
            //报名处理情况
            ArrayList lstStatus = new ArrayList();
            lstStatus.Add(new ListItem("不限处理情况", "-1"));
            lstStatus.Add(new ListItem("未处理", "0"));
            lstStatus.Add(new ListItem("已查看", "1"));
            lstStatus.Add(new ListItem("已录取", "2"));
            lstStatus.Add(new ListItem("未录取", "3"));
            ddlStatus1.DataSource = lstStatus;
            ddlStatus1.DataTextField = "Text";
            ddlStatus1.DataValueField = "Value";
            ddlStatus1.DataBind();

            ddlNurserySchool.Items.Clear();
            ddlNurserySchool.Items.Add(new FineUI.ListItem("龙岗区机关幼儿园本部", "0"));
            ddlNurserySchool.Items.Add(new FineUI.ListItem("龙岗区机关幼儿园龙城分园", "1"));
            ddlNurserySchool.Items.Add(new FineUI.ListItem("龙岗区机关幼儿园南湾分园", "2"));
            ddlNurserySchool.Items.Add(new FineUI.ListItem("龙岗区机关幼儿园坂田分园", "3"));
            ddlNurserySchool.Items.Insert(0, new FineUI.ListItem("所有幼儿园", "-1"));
        }
    }
    private void DataBinds()
    {
        string strKeywords = SqlHelper.String(ttboxKeywords.Text);
        string strCensusClass = SqlHelper.String(ddlCensusClass.SelectedValue);
        string strStatus = SqlHelper.String(ddlStatus1.SelectedValue);
        string strNurserySchool = SqlHelper.String(ddlNurserySchool.SelectedValue);
        if (!string.IsNullOrEmpty(strKeywords))
            strQuery = " and (UserName like '%" + strKeywords + "%' or IdCardNo like '%" + strKeywords + "%' or FatherName like '%" + strKeywords + "%' or FatherTel like '%" + strKeywords + "%' or MotherName like '%" + strKeywords + "%' or MotherTel like '%" + strKeywords + "%' or SignName like '%" + strKeywords + "%')";
        if (!string.IsNullOrEmpty(strCensusClass) && strCensusClass != "-1")
            strQuery += " and CensusClass=" + strCensusClass;
        if (!string.IsNullOrEmpty(strStatus) && strStatus != "-1")
            strQuery += " and Status=" + strStatus;
        if (!string.IsNullOrEmpty(strNurserySchool) && strNurserySchool != "-1")
            strQuery += " and NurserySchool=" + strNurserySchool;
        DataSet ds = WebBase.Comm.SelectByPage("BaoMing", "*", strQuery, "Id", "Id", 0, 1, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();
    }

    protected void ttboxKeywords_Trigger1Click(object sender, EventArgs e)
    {
        ttboxKeywords.Text = string.Empty;
    }
    protected void ttboxKeywords_Trigger2Click(object sender, EventArgs e)
    {
        //strQuery = string.Empty;
        //string strKeywords = SqlHelper.String(ttboxKeywords.Text.Trim());
        //if (!string.IsNullOrEmpty(strKeywords))
        //    strQuery += " and (aTitle like '%" + strKeywords + "%' or aNote like '%" + strKeywords + "%')";

        DataBinds();
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int iPageSize = 20;
        int.TryParse(ddlPageSize.SelectedValue, out iPageSize);
        gridList.PageSize = iPageSize;
        DataBinds();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DataBinds();
    }
    protected void gridList_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {
        gridList.PageIndex = e.NewPageIndex;
        DataBinds();
    }
    protected void gridList_RowCommand(object sender, FineUI.GridCommandEventArgs e)
    {
        int iSelectId = 0;
        Dictionary<int, Dictionary<int, string>> modifiedDict = gridList.GetModifiedDict();
        string strCommand = e.CommandName;
        int iResult = 0;
        string strResult = string.Empty;
        if (!string.IsNullOrEmpty(strCommand))
        {
            int iSelectIndexId = e.RowIndex;
            string strSelectId = gridList.Rows[iSelectIndexId].DataKeys[0].ToString();
            Int32.TryParse(strSelectId, out iSelectId);

            switch (strCommand)
            {
                case "Delete":
                    iResult = DalEx.BaoMingEx.Delete(" and Id=" + iSelectId, ref strResult);
                    break;
                case "Edit":

                    break;
            }
            if (iResult > 0)
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
                DataBinds();
            }
            else
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Warning);
            }
        }
    }
    protected void btnImportExcel_Click(object sender, EventArgs e)
    {
        string strKeywords = SqlHelper.String(ttboxKeywords.Text);
        string strCensusClass = SqlHelper.String(ddlCensusClass.SelectedValue);
        string strStatus = SqlHelper.String(ddlStatus1.SelectedValue);
        string strNurserySchool = SqlHelper.String(ddlNurserySchool.SelectedValue);
        if (!string.IsNullOrEmpty(strKeywords))
            strQuery = " and (UserName like '%" + strKeywords + "%' or IdCardNo like '%" + strKeywords + "%' or FatherName like '%" + strKeywords + "%' or FatherTel like '%" + strKeywords + "%' or MotherName like '%" + strKeywords + "%' or MotherTel like '%" + strKeywords + "%' or SignName like '%" + strKeywords + "%')";
        if (!string.IsNullOrEmpty(strCensusClass) && strCensusClass != "-1")
            strQuery += " and CensusClass=" + strCensusClass;
        if (!string.IsNullOrEmpty(strStatus) && strStatus != "-1")
            strQuery += " and Status=" + strStatus;
        if (!string.IsNullOrEmpty(strNurserySchool) && strNurserySchool != "-1")
            strQuery += " and NurserySchool=" + strNurserySchool;
        string strSql = "select OrderNO,UserName,Sex,Birthday,PlaceOrigin,CensusClass,CensusAddr,CensusArea,CensusPoliceStation,RegClass,IdCardNo,HouseAddr,(case HouseNature when 1 then '购房' when 2 then '租房' else '' end),NoteNo,(case HouseNature when 1 then (case b.HouseRegionID when 0 then '' else r.HouseRegionName end) else '' end) HouseRegionName,(case HouseNature when 1 then (case b.HouseRegionID when 0 then '' else g.HouseGroupName end)  else '' end) HouseGroupName,(case HouseNature when 1 then (case b.HouseRegionID when 0 then '' else (n.HouseNumberName+ isNull(n1.HouseNumberName,'')+ isnull(n2.HouseNumberName,'') + '室')  end)  else '' end),(case NurserySchool when 0 then '龙岗区机关幼儿园本部' when 1 then '龙岗区机关幼儿园龙城分园' when 2 then '龙岗区机关幼儿园南湾分园' when 3 then '龙岗区机关幼儿园坂田分园' else '' end),(case Planning when 0 then '独生子女' when 4 then '政策内二孩' when 5 then '政策外但已接受处理的幼儿' end) Planning,FatherName,FatherWorks,FatherTel,FatherCardNo,(case fatherInSZ when 0 then '否' when 1 then '是' else '' end),(case fatherPayJZZ when 0 then '否' when 1 then '是' else '' end),MotherName,MotherWorks,MotherTel,MotherCardNo,(case MotherInSZ when 0 then '否' when 1 then '是' else '' end),(case MotherPayJZZ when 0 then '否' when 1 then '是' else '' end),SignDate,Status from BaoMing b left join dbHouseRegion r on r.HouseRegionID = b.HouseRegionID left join dbHouseGroup g on g.HouseGroupID = b.HouseGroupID left join dbHouseNumber n on n.HouseNumberID = b.HouseNumber left join dbHouseNumber n1 on n1.HouseNumberID = b.HouseNumber1 left join dbHouseNumber n2 on n2.HouseNumberID = b.HouseNumber2  where 1=1" + strQuery;
        DataSet ds = SqlHelper.Select(strSql);
        string[] arrTitle = new string[] { "报名编号", "姓名", "性别", "出生日期", "籍贯", "户籍类型", "户口所在地", "户口所在地:区（县）", "户口所在地:派出所", "申请班级", "儿童身份证号", "现家庭住址", "住房性质", "不动产登记号/租赁合同号", "现家庭住址:小区", "现家庭住址:楼栋", "现家庭住址:房号", "报读学校", "家庭计划生育情况", "父亲姓名", "父亲工作单位", "联系电话", "父亲身份证号", "父亲是否深户", "父亲是否办理深圳市居住证", "母亲姓名", "母亲工作单位", "联系电话", "母亲身份证号", "母亲是否深户", "母亲是否办理深圳市居住证", "填写时间", "状态" };
        ImportExcel(arrTitle, ds, "幼儿园新生报名表");        
    }
    protected void btnSelectDelete_Click(object sender, EventArgs e)
    {
        string strResult = string.Empty;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        foreach (int iSelectId in lstSelectRowId)
        {
            DalEx.BaoMingEx.Delete(" and Id=" + iSelectId, ref strResult);
        }
        FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
        DataBinds();
    }
    protected void btnViewSign_Click(object sender, EventArgs e)
    {
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count > 0)
        {
            if (lstSelectRowId.Count > 1)
            {
                FineUI.Alert.Show("您只能选择一条报名信息进行查看！", FineUI.MessageBoxIcon.Warning);
            }
            else
            {
                winViews.IFrameUrl = "ViewSignTable.aspx?Id=" + lstSelectRowId[0];
                winViews.Hidden = false;
            }
        }
        else
        {
            FineUI.Alert.Show("请选择要操作的订单！", FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnCheckSave_Click(object sender, EventArgs e)
    {
        int iStatus = 0;
        string strCheckInfos = SqlHelper.String(txtCheckInfo.Text);
        int.TryParse(ddlStatus.SelectedValue, out iStatus);
        int iSelectId = 0;
        int.TryParse(hfSelectId.Text, out iSelectId);
        string strClassNum = SqlHelper.String(txtClassNum.Text);
        int iClassNo = 0;
        int.TryParse(txtSiteNo.Text, out iClassNo);
        bool bResult = false;
        bResult = iStatus == 2 ? DalEx.BaoMingEx.UpdateStatus(iSelectId, iStatus, strClassNum, iClassNo, strCheckInfos) : DalEx.BaoMingEx.UpdateStatus(iSelectId, iStatus, strCheckInfos);
        if (bResult)
        {
            FineUI.Alert.Show("报名表处理成功！", FineUI.MessageBoxIcon.Information);
            #region 发送邮件告知报名成功
            if (iStatus == 2)//发送邮件告知报名成功
            {

            }
            #endregion
        }
        else
        {
            FineUI.Alert.Show("报名表处理失败！", FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {
        int iSelectId = 0;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count > 0)
        {
            if (lstSelectRowId.Count > 1)
            {
                FineUI.Alert.Show("您只能选择一条报名信息进行操作！", FineUI.MessageBoxIcon.Warning);
            }
            else
            {
                ArrayList lstStatus = new ArrayList();
                lstStatus.Add(new ListItem("未处理", "0"));
                lstStatus.Add(new ListItem("已查看", "1"));
                lstStatus.Add(new ListItem("已录取", "2"));
                lstStatus.Add(new ListItem("未录取", "3"));
                ddlStatus.DataSource = lstStatus;
                ddlStatus.DataTextField = "Text";
                ddlStatus.DataValueField = "Value";
                ddlStatus.DataBind();
                //
                iSelectId = lstSelectRowId[0];
                Model.BaoMing baoMing = DalEx.BaoMingEx.Select(iSelectId);
                if (baoMing != null)
                {
                    ddlStatus.SelectedValue = baoMing.Status.ToString();
                    txtCheckInfo.Text = baoMing.CheckInfos;
                    hfSelectId.Text = iSelectId.ToString();

                    if (baoMing.Status == 2)
                    {
                        txtClassNum.Text = baoMing.ClassNum;
                        txtSiteNo.Text = baoMing.ClassNo;
                    }
                }
                winOrderCheck.Hidden = false;
            }
        }
        else
        {
            FineUI.Alert.Show("请选择要操作的订单！", FineUI.MessageBoxIcon.Warning);
        }
    }
    #region 属性转化
    protected string GetSex(object objValue)
    {
        int iValue = Convert.ToInt32(objValue);
        return iValue == 1 ? "男" : "女";
    }
    protected string GetStatus(object objValue)
    {
        string strResult = string.Empty;
        int iValue = Convert.ToInt32(objValue);
        switch (iValue)
        {
            case 0:
                strResult = "未处理";
                break;
            case 1:
                strResult = "已查看";
                break;
            case 2:
                strResult = "已录取";
                break;
            case 3:
                strResult = "未录取";
                break;
        }
        return strResult;
    }
    protected string GetClassType(object objValue)
    {
        string strResult = string.Empty;
        int iValue = Convert.ToInt32(objValue);
        switch (iValue)
        {
            case 0:
                strResult = "小班";
                break;
            case 1:
                strResult = "中班";
                break;
            case 2:
                strResult = "大班";
                break;
            case 3:
                strResult = "大大班";
                break;
        }
        return strResult;
    }
    protected string GetCensusClass(object objValue)
    {
        string strResult = string.Empty;
        int iValue = Convert.ToInt32(objValue);
        switch (iValue)
        { 
            //case 0:
            //    strResult = "龙岗";
            //    break;
            case 1:
                strResult = "深户";
                break;
            case 2:
                strResult = "非深户";
                break;
            case 3:
                strResult = "港籍";
                break;
            case 4:
                strResult = "台籍";
                break;
            case 5:
                strResult = "澳籍";
                break;
            //case 6:
            //    strResult = "外籍";
            //    break;
        }
        return strResult;
    }
    protected string GetNurserySchool(object objValue)
    {
        string strResult = string.Empty;
        int iValue = Convert.ToInt32(objValue);
        switch (iValue)
        {
            case 0:
                strResult = "龙岗区机关幼儿园本部";
                break;
            case 1:
                strResult = "龙岗区机关幼儿园龙城分园";
                break;
            case 2:
                strResult = "龙岗区机关幼儿园南湾分园";
                break;
            case 3:
                strResult = "龙岗区机关幼儿园坂田分园";
                break;
        }
        return strResult;
    }
    #endregion
    #region 导出Excel
    /// <summary>
    /// DataSet导出Excel
    /// </summary>
    /// <param name="arrTitle">列标题，若为null，则直接取dataset列标题</param>
    /// <param name="ds">要导出的DataSet</param>
    /// <param name="fileName">Excel文件名，不需要传入扩展名</param>
    private void ImportExcel(string[] arrTitle, DataSet ds, string fileName)
    {
        StringBuilder strb = new StringBuilder();
        strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
        strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
        strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"");
        strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
        strb.Append(" <style>");
        strb.Append(".xl26");
        strb.Append(" {mso-style-parent:style0;");
        strb.Append(" font-family:\"Times New Roman\", serif;");
        strb.Append(" mso-font-charset:0;");
        strb.Append(" mso-number-format:\"@\";}");
        strb.Append(" </style>");
        strb.Append(" <xml>");
        strb.Append(" <x:ExcelWorkbook>");
        strb.Append("  <x:ExcelWorksheets>");
        strb.Append("  <x:ExcelWorksheet>");
        strb.Append("    <x:Name>Sheet1 </x:Name>");
        strb.Append("    <x:WorksheetOptions>");
        strb.Append("    <x:DefaultRowHeight>285 </x:DefaultRowHeight>");
        strb.Append("    <x:Selected/>");
        strb.Append("    <x:Panes>");
        strb.Append("      <x:Pane>");
        strb.Append("      <x:Number>3 </x:Number>");
        strb.Append("      <x:ActiveCol>1 </x:ActiveCol>");
        strb.Append("      </x:Pane>");
        strb.Append("    </x:Panes>");
        strb.Append("    <x:ProtectContents>False </x:ProtectContents>");
        strb.Append("    <x:ProtectObjects>False </x:ProtectObjects>");
        strb.Append("    <x:ProtectScenarios>False </x:ProtectScenarios>");
        strb.Append("    </x:WorksheetOptions>");
        strb.Append("  </x:ExcelWorksheet>");
        strb.Append("  <x:WindowHeight>6750 </x:WindowHeight>");
        strb.Append("  <x:WindowWidth>10620 </x:WindowWidth>");
        strb.Append("  <x:WindowTopX>480 </x:WindowTopX>");
        strb.Append("  <x:WindowTopY>75 </x:WindowTopY>");
        strb.Append("  <x:ProtectStructure>False </x:ProtectStructure>");
        strb.Append("  <x:ProtectWindows>False </x:ProtectWindows>");
        strb.Append(" </x:ExcelWorkbook>");
        strb.Append(" </xml>");
        strb.Append("");
        strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed' border=\"1\"> <tr>");
        strb.Append("<td colspan=\"" + arrTitle.Length + "\" align=\"center\" style=\"height:65px;font-size:28px;\"><b>" + fileName + "&nbsp;(" + DateTime.Now.ToString("yyyy-MM-dd") + ")</b></td></tr>");

        if (ds.Tables.Count > 0)
        {
            //写列标题  
            if (arrTitle != null && arrTitle.Length > 0)
            {
                strb.Append("<tr style=\"height:55px;font-size:18px;\">");
                foreach (string strCol in arrTitle)
                {
                    strb.Append(" <td> <b> <center>" + strCol + "</center> </b> </td>");
                }
                strb.Append(" </tr>");
            }
            else
            {
                strb.Append("<tr style=\"height:55px;font-size:18px;\">");
                int columncount = ds.Tables[0].Columns.Count;
                for (int columi = 0; columi < columncount; columi++)
                {
                    strb.Append(" <td> <b> <center>" + ds.Tables[0].Columns[columi] + "</center> </b> </td>");
                }
                strb.Append(" </tr>");
            }

            //写数据     
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                strb.Append(" <tr style=\"height:45px;font-size:16px;\">");
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    //OrderNO,UserName,Sex,Birthday,PlaceOrigin,CensusClass,RegClass,IdCardNo,SignDate,Status
                    switch (ds.Tables[0].Columns[j].ToString())
                    {
                        case "Sex":
                            strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\" align=\"center\">" + GetSex(ds.Tables[0].Rows[i][j]) + " </td>");
                            break;
                        case "CensusClass":
                            strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\" align=\"center\">" + GetCensusClass(ds.Tables[0].Rows[i][j]) + " </td>");
                            break;
                        case "RegClass":
                            strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\" align=\"center\">" + GetClassType(ds.Tables[0].Rows[i][j]) + " </td>");
                            break;
                        case "Status":
                            strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\" align=\"center\">" + GetStatus(ds.Tables[0].Rows[i][j]) + " </td>");
                            break;
                        case "Birthday":
                            strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\" align=\"center\">" + Convert.ToDateTime(ds.Tables[0].Rows[i][j]).ToShortDateString() + " </td>");
                            break;
                        default:
                            strb.Append(" <td style=\"vnd.ms-excel.numberformat:@\" align=\"center\">" + ds.Tables[0].Rows[i][j].ToString() + " </td>");
                            break;
                    }
                }
                strb.Append(" </tr>");
            }
        }
        strb.Append(" </body> </html>");
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文   
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。   
        this.EnableViewState = false;
        Response.Write(strb);
        Response.End();
    }
    #endregion
}
